package com.party.core.service.copartner.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.copartner.TargetCopartnerReadDao;
import com.party.core.dao.write.copartner.TargetCopartnerWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.copartner.TargetCopartner;
import com.party.core.service.copartner.ITargetCopartnerService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Administrator.
 * @Description: 目标与合伙人关联表
 * @Date:Created in 2017/10/23 0023 下午 15:41.
 */
@Service
public class TargetCopartnerService implements ITargetCopartnerService {
    @Autowired
    private TargetCopartnerReadDao targetCopartnerReadDao;
    @Autowired
    private TargetCopartnerWriteDao targetCopartnerWriteDao;

    @Override
    public String insert(TargetCopartner t) {
        BaseModel.preInsert(t);
        boolean result = targetCopartnerWriteDao.insert(t);
        if (result) {
            return t.getId();
        }
        return null;
    }

    @Override
    public boolean update(TargetCopartner t) {
        t.setUpdateDate(new Date());
        return targetCopartnerWriteDao.update(t);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return targetCopartnerWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return targetCopartnerWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return targetCopartnerWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return targetCopartnerWriteDao.batchDelete(ids);
    }

    @Override
    public TargetCopartner get(String id) {
        return targetCopartnerReadDao.get(id);
    }

    @Override
    public List<TargetCopartner> listPage(TargetCopartner t, Page page) {
        return targetCopartnerReadDao.listPage(t, page);
    }

    @Override
    public List<TargetCopartner> list(TargetCopartner t) {
        return targetCopartnerReadDao.listPage(t, null);
    }

    @Override
    public List<TargetCopartner> batchList(Set<String> ids, TargetCopartner t, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return targetCopartnerReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public TargetCopartner findByTargetId(String targetId) {
        TargetCopartner targetCopartner = new TargetCopartner();
        targetCopartner.setTargetId(targetId);
        return targetCopartnerReadDao.getUnique(targetCopartner);
    }

    @Override
    public List<TargetCopartner> findByCreatedId(String createdId) {
        TargetCopartner targetCopartner = new TargetCopartner();
        targetCopartner.setCreatedId(createdId);
        return targetCopartnerReadDao.listPage(targetCopartner, null);
    }

    @Override
    public List<TargetCopartner> findByPartnerId(String partnerId) {
        TargetCopartner targetCopartner = new TargetCopartner();
        targetCopartner.setPartnerId(partnerId);
        return targetCopartnerReadDao.listPage(targetCopartner, null);
    }

    @Override
    public List<Map<String, Object>> webListPage(Map<String, Object> params, Page page) {
        return targetCopartnerReadDao.webListPage(params, page);
    }

    @Override
    public void deleteByTargetId(String targetId) {
        targetCopartnerWriteDao.deleteByTargetId(targetId);
    }
}
